package com.klicen.vus.insurance.util;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class HttpClientUtil {
	private static String reqUrl = "http://xy.5i4s.com/insurance_service/pub/renewal?cityCode=510100&licenseNo=川U88566";
	public static void get() {
		long begin = System.currentTimeMillis();
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpGet httpGet=new HttpGet(reqUrl);//HTTP Get请求(POST雷同)
		httpGet.setHeader("Content-type", "application/json; charset=utf-8");
		httpGet.setHeader("Connection", "Close");
		httpGet.setHeader("clientKey", "e0ff6724e12405e4286ce327fc84cae3");
		httpGet.setHeader("shopCode", "cd");
		RequestConfig requestConfig = RequestConfig.custom()
				.setSocketTimeout(60000)
				.setConnectTimeout(60000)
				.build();//设置请求和传输超时时间
		httpGet.setConfig(requestConfig);
		try {
			CloseableHttpResponse response = httpClient.execute(httpGet);
			StatusLine statusLine = response.getStatusLine();
			System.out.println(statusLine);
			HttpEntity entity = response.getEntity();
			System.out.println(EntityUtils.toString(entity, "UTF-8"));
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}//执行请求
		long end = System.currentTimeMillis();
		System.out.println(end-begin);
	}
	public static void post() {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(reqUrl);
		// 构造消息头
		httpPost.setHeader("Content-type", "application/json; charset=utf-8");
		httpPost.setHeader("Connection", "Close");
		httpPost.setHeader("clientKey", "e0ff6724e12405e4286ce327fc84cae3");
		httpPost.setHeader("shopCode", "cd");
		RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build();//设置请求和传输超时时间
		httpPost.setConfig(requestConfig);
		try {
			CloseableHttpResponse response = httpClient.execute(httpPost);
			StatusLine statusLine = response.getStatusLine();
			System.out.println(statusLine);
			HttpEntity entity = response.getEntity();
			System.out.println(EntityUtils.toString(entity, "UTF-8"));
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		get();
//		post();
	}
}
